#include <Comm/Include/SimpleMessage.h>

// STL
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

SimpleMessage::SimpleMessage(const char* _data, unsigned int size)
{    
    this->data_size = size;
    this->data = new char[this->data_size];
    memcpy(this->data, _data, this->data_size);
}

SimpleMessage::SimpleMessage(ISerializable* s)
{
    std::stringstream stream;
    this->data_size = s->serialize(stream);
    this->data = new char[this->data_size];
    memcpy(this->data, stream.str().c_str(), this->data_size);
}

SimpleMessage::~SimpleMessage(void)
{
    delete this->data;
}

unsigned int SimpleMessage::getData(char* &_data)
{
    if (_data != 0)
    {
        delete _data;
    }
    _data = new char[this->data_size];
    memcpy(_data, this->data, this->data_size);
    return this->data_size;
}

unsigned int SimpleMessage::getData(std::stringstream& s)
{
    s.write(this->data, this->data_size);
    return this->data_size;
}

SimpleMessage* SimpleMessage::clonar()
{
    return new SimpleMessage(this->data, this->data_size);
}

